package com.hxht.storage.server.service.quartz.job;

import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerContext;
import org.quartz.SchedulerException;

import com.hxht.storage.server.bean.PlanRecordParamBean;
import com.hxht.storage.server.service.NvrDeviceService;

public class PlanRecordStartJob extends BaseNvrJob {

	private static Logger quartzLogger = Logger.getLogger(PlanRecordStartJob.class);
	
	private PlanRecordParamBean startParam;
	
	public void setStartParam(PlanRecordParamBean param){
		startParam = param;
	}
	
	@Override
	protected void executeInternal(JobExecutionContext context)
			throws JobExecutionException {

		//获取JobExecutionContext中的service对象    
        SchedulerContext schedCtx;
		try {
			schedCtx = context.getScheduler().getContext();
			//获取SchedulerContext中的service    
			//这里的service就是通过配置文件 配置的    
			nvrDeviceService = (NvrDeviceService)schedCtx.get("nvrDeviceService");
		
			// needSchedule 传入为false,表明，如果更新计划参数出错时，不需要再次增加定时任务去更新计划参数
			boolean initResult = nvrDeviceService.updatePlanRecord(sn, channel, startParam, false);
			if (initResult){
				context.getScheduler().pauseTrigger(context.getTrigger().getKey());     
			    context.getScheduler().unscheduleJob(context.getTrigger().getKey()); 
			    
			    quartzLogger.info("\nplan record start job successed!\n" +
						"sn = " + sn + " / ipchannel = " + channel);
			} else {
				quartzLogger.info("\nplan record start job failed!\n" +
						"sn = " + sn + " / ipchannel = " + channel);
			}
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			quartzLogger.error("plan record start job error!", e);
		}     
	}

}
